/*     */ package com.newcrm.webapi;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.QisoukeActivationCode;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.QisoukeManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.JsoupManager;
/*     */ import com.newcrm.util.ParameterUtil;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.sub.QisoukeSaveCustomer;
/*     */ import com.newcrm.webapi.sub.QisoukeSearchCompany;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.List;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.jsoup.nodes.Document;
/*     */ 
/*     */ public class QisoukeServlet extends AbstractHttpServlet
/*     */ {
/*     */   private static final long serialVersionUID = -5226616041947007411L;
/*  43 */   private static Logger logger = Logger.getLogger(QisoukeServlet.class
/*  44 */     .getName());
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  67 */     if (!checkLogin(request)) {
/*  68 */       super.showLoginPage(request, response);
/*  69 */       return;
/*     */     }
/*     */ 
/*  73 */     User user = super.getLoginUser(request);
/*  74 */     CompanyInfo company = super.getCompany(user.companyId);
/*  75 */     if (!company.qisoukeActivationCode.hasSearhAuth(user.userId)) {
/*  76 */       request.getRequestDispatcher("/qisouke_intro.jsp").forward(request, 
/*  77 */         response);
/*  78 */       return;
/*     */     }
/*     */ 
/*  81 */     String url = request.getRequestURI();
/*  82 */     String[] param = ParameterUtil.getParameters(url, getTagkey());
/*     */ 
/*  84 */     if ((param == null) || (param.length == 0)) {
/*  85 */       request.getRequestDispatcher("/qisouke.jsp").forward(request, 
/*  86 */         response);
/*  87 */       return;
/*     */     }
/*     */ 
/*  90 */     if (param.length == 1)
/*     */     {
/*  92 */       if ("search".equals(param[0])) {
/*     */         try {
/*  94 */           new QisoukeSearchCompany(this).parse(request, response);
/*     */         } catch (NotFoundException e) {
/*  96 */           logger.warn(e);
/*  97 */           super.showErrorMessage(response, "搜索超时，请稍后再试");
/*  98 */           return;
/*     */         }
/*     */ 
/* 101 */         return;
/*     */       }
/*     */ 
/* 105 */       if ("save".equals(param[0])) {
/* 106 */         response.setContentType("text/html;charset=utf-8");
/* 107 */         PrintWriter out = response.getWriter();
/*     */         try {
/* 109 */           new QisoukeSaveCustomer(this).parse(request, response);
/*     */         } catch (Exception e) {
/* 111 */           JSONObject jsonObject = new JSONObject();
/* 112 */           jsonObject.put("error", "1");
/* 113 */           jsonObject.put("msg", "搜索超时，请稍后再试");
/* 114 */           out.println(jsonObject);
/*     */         }
/* 116 */         return;
/*     */       }
/*     */ 
/* 120 */       if ("history".equals(param[0])) {
/* 121 */         response.setContentType("text/html;charset=utf-8");
/* 122 */         PrintWriter out = response.getWriter();
/* 123 */         JSONObject jsonObject = new JSONObject();
/*     */ 
/* 125 */         QisoukeManager manager = new QisoukeManager();
/*     */ 
/* 127 */         List items = manager.list(user.companyId, 
/* 128 */           user.userId);
/* 129 */         if (items == null) {
/* 130 */           jsonObject.put("msg", "获得搜索历史失败");
/* 131 */           jsonObject.put("error", "1");
/*     */         } else {
/* 133 */           jsonObject.put("items", items);
/* 134 */           jsonObject.put("error", "0");
/*     */         }
/*     */ 
/* 137 */         out.println(jsonObject);
/* 138 */         out.flush();
/* 139 */         out.close();
/* 140 */         return;
/*     */       }
/*     */ 
/* 144 */       if ("checkNew".equals(param[0])) {
/* 145 */         String companyName = Util.getURLParameter(request
/* 146 */           .getParameter("companyName"));
/*     */ 
/* 148 */         if ((companyName == null) || (companyName.trim().length() == 0)) {
/* 149 */           super.showErrorMessage(response, "数据格式错误，没有公司/联系人");
/* 150 */           return;
/*     */         }
/*     */ 
/* 154 */         CustomerManager manager = new CustomerManager();
/* 155 */         response.setContentType("text/html;charset=utf-8");
/* 156 */         PrintWriter out = response.getWriter();
/*     */         try {
/* 158 */           int customerId = manager.getCustomerId(user.companyId, 
/* 159 */             companyName);
/* 160 */           if (customerId != 0) {
/* 161 */             JSONObject jsonObject = new JSONObject();
/* 162 */             jsonObject.put("error", "1");
/* 163 */             jsonObject.put("customerId", Integer.valueOf(customerId));
/* 164 */             out.println(jsonObject);
/* 165 */             return;
/*     */           }
/*     */         }
/*     */         catch (DBException ex) {
/* 169 */           super.showErrorMessage(response, "检索数据库失败，请联系管理员！");
/* 170 */           return;
/*     */         }
/* 172 */         JSONObject jsonObject = new JSONObject();
/* 173 */         jsonObject.put("error", "0");
/* 174 */         jsonObject.put("msg", "新客户");
/* 175 */         out.println(jsonObject);
/* 176 */         out.flush();
/* 177 */         out.close();
/* 178 */         return;
/*     */       }
/*     */ 
/* 181 */       return;
/*     */     }
/*     */ 
/* 184 */     if ((param.length == 2) && 
/* 185 */       ("company".equals(param[0]))) {
/* 186 */       String id = param[1];
/* 187 */       String searchUrl = "http://so.qikebao.com/qisouke/" + id;
/* 188 */       response.setContentType("text/html;charset=utf-8");
/* 189 */       PrintWriter out = response.getWriter();
/*     */       try
/*     */       {
/* 192 */         Document doc = JsoupManager.getSearchResult(user, company, 
/* 193 */           searchUrl.toString());
/* 194 */         out.println(doc.text());
/*     */       }
/*     */       catch (Exception e) {
/*     */         try {
/* 198 */           Thread.sleep(1000L);
/*     */         }
/*     */         catch (InterruptedException localInterruptedException) {
/*     */         }
/*     */         try {
/* 203 */           Document doc = JsoupManager.getSearchResult(user, 
/* 204 */             company, searchUrl.toString());
/* 205 */           out.println(doc.text());
/*     */         } catch (Exception e2) {
/* 207 */           JSONObject jsonObject = new JSONObject();
/* 208 */           jsonObject.put("error", "1");
/* 209 */           jsonObject.put("msg", "搜索超时，请稍后再试");
/* 210 */           out.println(jsonObject);
/*     */         }
/*     */       }
/* 213 */       out.flush();
/* 214 */       out.close();
/* 215 */       return;
/*     */     }
/*     */   }
/*     */ 
/*     */   protected String getTagkey()
/*     */   {
/* 222 */     return "/qisouke";
/*     */   }
/*     */ 
/*     */   protected AbstractAction getAddAction()
/*     */   {
/* 227 */     return null;
/*     */   }
/*     */ 
/*     */   protected AbstractAction getListAction()
/*     */   {
/* 232 */     return null;
/*     */   }
/*     */ 
/*     */   protected AbstractAction getShowAddPageAction()
/*     */   {
/* 237 */     return null;
/*     */   }
/*     */ 
/*     */   protected AbstractAction getUpdateAction(String id)
/*     */   {
/* 242 */     return null;
/*     */   }
/*     */ 
/*     */   protected AbstractAction getShowConfirmDestroyAction(String id)
/*     */   {
/* 247 */     return null;
/*     */   }
/*     */ 
/*     */   protected AbstractAction getDeleteAction(String id)
/*     */   {
/* 252 */     return null;
/*     */   }
/*     */ 
/*     */   protected AbstractAction getShowAction(String id)
/*     */   {
/* 257 */     return null;
/*     */   }
/*     */ 
/*     */   protected AbstractAction getShowUpdatePageAction(String id)
/*     */   {
/* 262 */     return null;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.QisoukeServlet
 * JD-Core Version:    0.6.1
 */